package com.vlyman.base.controller;

import com.vlyman.base.model.entity.User;
import com.vlyman.base.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 类描述：
 *  demo，用户控制类
 * @author chenjs
 * @date 2018/9/16
 */
@RestController
@RequestMapping("/api/demo/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/list")
    public List<User> getAllUsers(){
        try {
            return userService.getAllUser();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @PostMapping("")
    public String saveUser(User user){
        try {
            return userService.saveUser(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "Error";
    }

    @RequestMapping(value = "/rm", method = RequestMethod.POST)
    public String saveUserRM(User user){
        try {
            return userService.saveUser(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "Error";
    }

    @PostMapping("/rb")
    @Transactional
    public String saveUserRB(@RequestBody User user){
        return userService.saveUser(user);
    }
}
